package com.gmail.hongyz.myfuse.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

import com.gmail.hongyz.myfuse.common.Constants;
import com.gmail.hongyz.myfuse.common.PageObject;
import com.gmail.hongyz.myfuse.model.Project;
import com.gmail.hongyz.myfuse.model.Role;
import com.gmail.hongyz.myfuse.model.User;
import com.gmail.hongyz.myfuse.model.UserRole;
import com.gmail.hongyz.myfuse.service.ProjectService;
import com.gmail.hongyz.myfuse.service.UserService;

public class ProjectController extends MultiActionController {

	private static final Log log = LogFactory.getLog(ProjectController.class);
	
	private ProjectService projectService;
	private UserService userService;

	public void query(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
	
		if (log.isDebugEnabled()) {
	        log.debug("entering 'handleRequest' method...");
	    }
		PageObject pageObject = PageObject.parseParams(request);
		List<Project> projects = projectService.getProjects(pageObject);
		JSONObject result = new JSONObject();
		JSONArray list = JSONArray.fromObject(projects);
		result.accumulate(Constants.EXT_PAGING_TOTAL_COUNT, pageObject.getTotal());
		result.accumulate(Constants.EXT_PAGING_CONTENT, list);
		response.getWriter().println(result.toString());
		response.getWriter().flush();
		response.getWriter().close();
	}

	public void setProjectService(ProjectService projectService) {
		this.projectService = projectService;
	}
	
	public ModelAndView init(HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		if (log.isDebugEnabled()) {
            log.debug("entering 'handleRequest' method...");
        }
		return new ModelAndView("project");
	}
	
	public void loadUserRoles(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
	
		if (log.isDebugEnabled()) {
	        log.debug("entering 'handleRequest' method...");
	    }
		String projectId = request.getParameter("projectId");
		List<UserRole> userRoles = projectService.getUserRoles(projectId);
		JSONObject result = new JSONObject();
		JSONArray list = JSONArray.fromObject(userRoles);
		result.accumulate(Constants.EXT_PAGING_CONTENT, list);
		response.getWriter().println(result.toString());
		response.getWriter().flush();
		response.getWriter().close();
	}

	
	public void save(HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		if (log.isDebugEnabled()) {
            log.debug("entering 'handleRequest' method...");
        }
		
		String type = request.getParameter("type");
		String id = request.getParameter("id");
		String name = request.getParameter("name");
		String desc = request.getParameter("desc");
		log.debug("name: " + name);
		log.debug("type: " + type);
		if ("new".equals(type)) {
			Project project = new Project();
			project.setName(name);
			project.setDescription(desc);
			log.info("new project: " + project);
			projectService.saveProject(project);
		} else {
			Project project = projectService.getProject(id);
			project.setName(name);
			project.setDescription(desc);
			log.info("edit project: " + project);
			projectService.saveProject(project);
		}
		
//		JSONObject result = new JSONObject();
//		response.getWriter().println(result.toString());
//		response.getWriter().flush();
//		response.getWriter().close();
	}
	public void addUserRole(HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		if (log.isDebugEnabled()) {
            log.debug("entering 'handleRequest' method...");
        }
		String projectId = request.getParameter("projectId");
		String userId = request.getParameter("userId");
		String role = request.getParameter("role");
		Project project = projectService.getProject(projectId);
		User user = userService.getUser(userId);
		
		UserRole userRole = new UserRole();
		userRole.setProject(project);
		userRole.setUser(user);
		userRole.setRole(Role.getRole(role));
		
		JSONObject result = new JSONObject();
		response.getWriter().println(result.toString());
		response.getWriter().flush();
		response.getWriter().close();
	}
}
